<?php
class StringHelper
{
	/**
	 * 支持任意位置截取中文字符串
	 */
	static function strcut($string, $sublen, $start = 0)
	{
		$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
		preg_match_all($pa, $string, $t_string);
		//if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
		return join('', array_slice($t_string[0], $start, $sublen));
	}
	
	/**
	 * 转换字符串为utf-8编码
	 */
	static function encode($cont)
	{
		$encode	=	mb_detect_encoding($cont, 'GB2312, UTF-8, GBK');
		if($encode!='UTF-8')
		{
			return iconv($encode, 'UTF-8//TRANSLIT//IGNORE', $cont);
		}
		return $cont;
	}
	
	/**
	 * 从指定位置开始截取特定字符串位置的字符串
	 *
	 * @param string	$obj	要从$str中取的字符串
	 * @param string	$npre	目前字符串不能出现的前缀
	 * @param bool	$jumpFirst	跳过第一个字符串不判断
	 */
	static function shiftTo($str, $obj, $seek, $npre='', $jumpFirst=0)
	{
		$pos	=	$jumpFirst ? $seek + 1 : $seek;
		$nsize	=	strlen($npre);
		do{
			$pos	=	strpos($str, $obj, $pos);
			if($pos===false){
				return false;
			}
		} while($nsize && $npre==substr($str,$pos-$nsize,$nsize) && $pos++);
		
		return substr($str, $seek, $pos-$seek+strlen($obj));
	}

	/**
	 * 验证email
	 */
	static function email($e)
	{
		return preg_match('~^[\w\-.]+@[\w\-.]+\.[\w\-.]+$~', $e);
	}


}
?>
